import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HallDAO {
    /**
     * 获取所有放映厅
     */
    public static List<Hall> getAllHalls() {
        List<Hall> halls = new ArrayList<>();
        String sql = "SELECT * FROM halls";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                Hall hall = new Hall(
                        rs.getString("id"),
                        rs.getString("name"),
                        rs.getInt("rows"),
                        rs.getInt("cols")
                );
                // 获取该放映厅的所有座位
                hall.setSeats(SeatDAO.getSeatsByHallId(hall.getId()));
                halls.add(hall);
            }
        } catch (SQLException e) {
            System.err.println("查询所有放映厅失败: " + e.getMessage());
        }
        return halls;
    }

    /**
     * 根据ID查询放映厅
     */
    public static Hall getHallById(String id) {
        String sql = "SELECT * FROM halls WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                Hall hall = new Hall(
                        rs.getString("id"),
                        rs.getString("name"),
                        rs.getInt("rows"),
                        rs.getInt("cols")
                );
                hall.setSeats(SeatDAO.getSeatsByHallId(hall.getId()));
                return hall;
            }
        } catch (SQLException e) {
            System.err.println("查询放映厅失败: " + e.getMessage());
        }
        return null;
    }
}
